package com.interest.action;

import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import com.interest.util.SignUtil;
import com.interest.service.MessageService;
import com.interest.service.MessageServiceImp;
import com.interest.util.MessageUtil;

public class TokenAction implements Controller  {
	
	private MessageService messageService = new MessageServiceImp();
	@Override
	public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
	String method = 	request.getMethod().toLowerCase();
		if("get".equals(method)){
			// 微信加密签名  
	        String signature = request.getParameter("signature");  
	        // 时间戳  
	        String timestamp = request.getParameter("timestamp");  
	        // 随机数  
	        String nonce = request.getParameter("nonce");  
	        // 随机字符串  
	        String echostr = request.getParameter("echostr");  
	        System.out.println(signature);
	        PrintWriter out = response.getWriter();
	        if (SignUtil.checkSign(signature, timestamp, nonce)) {  
	            out.print(echostr);  
	        }  
		} else if ("post".equals(method)) {
			String respon = messageService.delMessage(request);
			response.getWriter().println(respon);
		}
		
		return null;
	}
	

}
